package global.com.loansapplication.ui.actviity.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tencent.connect.common.Constants;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import butterknife.Bind;
import butterknife.ButterKnife;
import global.com.loansapplication.R;
import global.com.loansapplication.bean.ResponInfo;
import global.com.loansapplication.util.ShareUtils;
import global.com.loansapplication.util.ToastUtils;

/**
 * Created by admin on 2017/9/27.
 */

public class ShareAct extends Activity implements View.OnClickListener, IUiListener {


    @Bind(R.id.activity_share_weixin)
    ImageView weixin;
    @Bind(R.id.activity_share_qq_friend)
    ImageView qqFriend;
    @Bind(R.id.activity_share_weixin_friend)
    ImageView weiCircle;
    @Bind(R.id.activity_share_qq_kongjian)
    ImageView qqKongjian;
    @Bind(R.id.activity_share_ll)
    LinearLayout shareLl;
    public static IWXAPI wxApi;
    ShareUtils shareUtils;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
        initView();
        setData();
        setListener();

    }

    @SuppressWarnings("deprecation")
    private void initView() {
        ResponInfo url = (ResponInfo) getIntent().getSerializableExtra("data");
        shareUtils = new ShareUtils(ShareAct.this, url);
        wxApi = WXAPIFactory.createWXAPI(this, ShareUtils.WX_APP_ID);
        shareUtils.setWxApi(wxApi);
    }

    private void setData() {

    }

    private void setListener() {
        shareLl.setOnClickListener(this);
        weixin.setOnClickListener(this);
        weiCircle.setOnClickListener(this);
        qqFriend.setOnClickListener(this);
        qqKongjian.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_share_ll:
                finish();
                break;
            case R.id.activity_share_weixin:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            shareUtils.wxShare();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();


                break;
            case R.id.activity_share_qq_friend:
                shareUtils.qqShare(ShareAct.this);

                break;
            case R.id.activity_share_weixin_friend:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            shareUtils.pyqShare();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                break;
            case R.id.activity_share_qq_kongjian:
                shareUtils.qzoneShare(ShareAct.this);

                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_QQ_SHARE || requestCode == Constants.REQUEST_QZONE_SHARE) {
            Tencent.onActivityResultData(requestCode, resultCode, data, this);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 从当前应用唤起微博并进行分享后，返回到当前应用时，需要在此处调用该函数
        // 来接收微博客户端返回的数据；执行成功，返回 true，并调用
        // {@link IWeiboHandler.Response#onResponse}；失败返回 false，不调用上述回调
    }


    @Override
    public void onComplete(Object o) {
        ToastUtils.shortToast("分享成功");

    }

    @Override
    public void onError(UiError uiError) {
        ToastUtils.shortToast("分享失败");
        ToastUtils.shortToast(uiError.errorMessage + uiError.errorCode);

    }

    @Override
    public void onCancel() {
        ToastUtils.shortToast("分享取消");

    }
}

